package com.tianyu.inspection.configuration;

import com.tianyu.inspection.common.enums.RoState;
import com.tianyu.inspection.entity.vo.Ro;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**********************************
 * @Author YSW
 * @Description 文件上传 MultipartException异常处理
 * @Date 2021/1/29 - 17:19
 **********************************/

@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    /**
     * 处理上传异常
     *
     * @param throwable
     * @return
     */
    @ExceptionHandler(MultipartException.class)
    public ResponseEntity<Ro<Void>> handleAll(Throwable throwable) throws Exception {
        logger.error(throwable.getMessage());
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/json;charset=UTF-8");
        return new ResponseEntity<>(new Ro<Void>(RoState.FAIL).setMessage("缺少参数,或上传文件过大"), headers, HttpStatus.OK);
    }
}